home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / minix / update~4.z / update~4 / lib_stdio_fputs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  1.9 KB  |  82 lines

  1. /*                f p u t s
  2.  *
  3.  * Write a string onto the specified stream. The terminating null
  4.  * character in the string is not written.
  5.  *
  6.  * The function returns EOF on error, otherwise it will return the
  7.  * last character written.
  8.  *
  9.  * Patchlevel 1.1
  10.  *
  11.  * Edit History:
  12.  * 05-Sep-1989    Change SETCLEANUP to SETIOFLUSH. Cast buffer
  13.  *        pointer to unsigned char *.
  14.  * 03-Sep-1989    Accommodate line buffered streams by flushing
  15.  *        afterwards. Don't even bother looking for '\n'.
  16.  *        Call NPUTC() for faster processing of unbuffered
  17.  *        streams.
  18.  * 02-Sep-1989    Speed up by writing directly to buffer.
  19.  */
  20.  
  21. #include "stdiolib.h"
  22.  
  23. /*LINTLIBRARY*/
  24.  
  25. int fputs(str, fp)
  26.  
  27. CONST char *str;                /* string */
  28. FILE       *fp;                    /* stream */
  29.  
  30. {
  31.   char flush;                /* flush line buffered stream */
  32.   int lastch;                /* last character */
  33.   unsigned int bytesleft;        /* bytes left in output buffer */
  34.   unsigned char *q;            /* output buffer pointer */
  35.   unsigned char *s;            /* input string */
  36.  
  37. /* Check for output buffer */
  38.   if (! HASBUFFER(fp)) {
  39.     if (_allocbuf(fp) < 0)
  40.       return EOF;
  41.     SETIOFLUSH();
  42.   }
  43.  
  44. /* Cast buffer pointer */
  45.   s = (unsigned char *) str;
  46.  
  47. /* Do unbuffered cases slowly */
  48.   if (TESTFLAG(fp, _IONBF)) {
  49.     for (lastch = 0; *s; lastch = (unsigned char) *s++) {
  50.       if (NPUTC(*s, fp) == EOF)
  51.     return EOF;
  52.     }
  53.     return lastch;
  54.   }
  55.  
  56. /* Buffered mode --- write directly to buffer */
  57.   for (lastch = s[0]; ; ) {
  58.     if ((bytesleft = UNUSEDINWRITEBUFFER(fp)) != 0) {
  59.       flush = 0;
  60.       q = GETWRITEPTR(fp);
  61.       if (TESTFLAG(fp, _IOLBF)) {
  62.     UNROLL_DO(fputslb, bytesleft,
  63.           if (*s == '\n') flush = 1;
  64.           if ((*q++ = *((unsigned char *) s)++) == 0) break);
  65.       }
  66.       else {
  67.     UNROLL_DO(fputsfb, bytesleft,
  68.           if ((*q++ = *((unsigned char *) s)++) == 0) break);
  69.       }
  70.       if (bytesleft) {
  71.     SETWRITEPTR(fp, q-1);
  72.     if (flush)
  73.       (void) fflush(fp);
  74.     return lastch ? s[-2] : 0;
  75.       }
  76.       SETWRITEPTR(fp, q);
  77.     }
  78.     if (fflush(fp) == EOF)
  79.       return EOF;
  80.   }
  81. }
  82.